package com.ailanyin.admin.controller;

import com.ailanyin.admin.service.SysLoginService;
import com.ailanyin.admin.service.SysMenuService;
import com.ailanyin.admin.service.SysPermissionService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.admin.vo.LoginBodyVo;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.model.SysMenu;
import com.ailanyin.model.SysUser;
import com.ailanyin.security.service.SecurityUserService;
import com.ailanyin.security.utils.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 15:54
 */
@RestController
@Api(tags = "登录管理")
public class SysLoginController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private SecurityUserService securityUserService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @GetMapping("/getRouters")
    @ApiOperation(value = "获取路由信息")
    public AjaxResult getRouters() {
        Long userId = sysUserService.getCurrentUser().getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(sysMenuService.buildMenus(menus));
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "获取用户信息")
    public AjaxResult getInfo() {
        SysUser user = sysUserService.getCurrentUser();
        // 角色集合
        Set<String> roles = sysPermissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = sysPermissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public AjaxResult login(@RequestBody LoginBodyVo loginBody) {
        return sysLoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());

    }

    @GetMapping("/captchaImage")
    @ApiOperation(value = "获取登录验证码")
    public AjaxResult getCaptchaImage() {
        return sysLoginService.getCaptchaImage();
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public AjaxResult register(@RequestBody LoginBodyVo register) {
        sysLoginService.register(register);
        return AjaxResult.success();
    }

    @PostMapping("/myLogout")
    @ApiOperation(value = "退出登录")
    public void logout(HttpServletRequest request) {
        String authHeader = request.getHeader(this.tokenHeader);
        if (authHeader != null && authHeader.startsWith(this.tokenHead)) {
            String authToken = authHeader.substring(this.tokenHead.length());
            String username = jwtTokenUtil.getUserNameFromToken(authToken);
            securityUserService.delUserDetailsFromMap(username);
        }
    }
}
